package com.springboot.bookmanegersystem.service.imp;

import com.springboot.bookmanegersystem.mapper.BookMapper;
import com.springboot.bookmanegersystem.model.BookInfo;
import com.springboot.bookmanegersystem.model.req.BookRequestParam;
import com.springboot.bookmanegersystem.model.resp.Result;
import com.springboot.bookmanegersystem.service.BookService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description :
 * User: jiume
 * Date: 2025-05-11
 * Time: 8:49
 */
@Slf4j
@Service
public class BookServiceImp implements BookService {

    @Resource
    private BookMapper bookMapper;

    @Override
    public Result getBookListDefault() {
        List<BookInfo> bookInfoList =  bookMapper.getBookListDefault();
        // 根据status的值对应statusCN赋值
        for (BookInfo bookInfo : bookInfoList) {
            if (bookInfo.getStatus() == 0) {
                bookInfo.setStatusCN("可借阅");
            } else {
                bookInfo.setStatusCN("不可借阅");
            }
        }
        log.info("[getBookListDefault]: 图书获取成功");
        return Result.success(bookInfoList);
    }

    @Override
    public Result getBookList(BookRequestParam bookRequestParam) {
        // 1、检验参数是否合法
        if (bookRequestParam == null) {
            log.info("[getBookList]: 参数不符合");
            return Result.error("参数不符合");
        }
        Integer currentPage = bookRequestParam.getCurrentPage();
        Integer pageSize = bookRequestParam.getPageSize();
        if (currentPage == null || pageSize == null || currentPage <= 0 || pageSize <= 0) {
            log.info("[getBookList]: 参数不符合");
            return Result.error("参数不符合");
        }
        // 2、查询数据库
        Integer newCurrentPage = (currentPage - 1) * pageSize;
        List<BookInfo> bookList = bookMapper.getBookList(newCurrentPage, pageSize);
        // 根据status的值对应statusCN赋值
        for (BookInfo bookInfo : bookList) {
            if (bookInfo.getStatus() == 0) {
                bookInfo.setStatusCN("可借阅");
            } else {
                bookInfo.setStatusCN("不可借阅");
            }
        }
        // 3、返回数据
        log.info("[getBookList]: 图书获取成功");
        return Result.success(bookList);
    }

    @Override
    public Result getBookCount() {
        Integer bookCount = bookMapper.getBookCount();
        log.info("[getBookCount]: 获取图书总数成功");
        return Result.success(bookCount);
    }

    @Override
    public Result deleteBook(Integer bookId) {
        // 1、检验参数
        // 这里不能简单通过 Id 是否 大于总数来判断，因为总数仅是未被删除
        if (bookId == null || bookId <= 0) {
            log.info("[deleteBook]: 参数不合法");
            return Result.error("参数不合法");
        }
        // 2、查询该图书是否已经被删除
        BookInfo bookInfo = queryBookById(bookId);
        if (bookInfo == null) {
            return Result.error("该图书已经被删除或该图书不存在");
        }
        Integer result = bookMapper.deleteBook(bookId);
        if (result == 1) {
            log.info("[getBookCount]: 删除的图书id是 "+bookId);
            return Result.success(true);
        }
        return Result.error("图书id是 "+bookId+" 的图书删除失败");
    }

    @Override
    public Result updateBook(BookInfo bookInfo) {
        // 1、检验参数
        if (bookInfo == null || queryBookById(bookInfo.getBookId()) == null) {
            log.info("[updateBook]: 参数不合法");
            return Result.error("参数不合法");
        }
        if (!StringUtils.hasText(bookInfo.getBookName())
                || !StringUtils.hasText(bookInfo.getAuthor())
                || !StringUtils.hasText(bookInfo.getPublish())) {
            log.info("[updateBook]: 参数不合法");
            return Result.error("参数不合法");
        }
        if (bookInfo.getCount() == null || bookInfo.getCount() < 0
                || bookInfo.getPrice() == null
                || bookInfo.getPrice().compareTo(new BigDecimal(0)) <= 0
                || bookInfo.getStatus() == null
                || bookInfo.getStatus() < 0
                || bookInfo.getStatus() > 2) {
            log.info("[updateBook]: 参数不合法");
            return Result.error("参数不合法");
        }
        // 2、更新数据
        Integer result = bookMapper.updateBook(bookInfo);
        if (result == 1) {
            log.info("[updateBook]: "+bookInfo.getBookId()+" 更新成功");
            return Result.success(true);
        }
        log.info("[updateBook]: "+bookInfo.getBookId()+" 更新失败");
        return Result.error(bookInfo.getBookId()+" 更新失败");
    }

    @Override
    public Result getBookById(Integer bookId) {
        BookInfo bookInfo = queryBookById(bookId);
        if (bookId == null) {
            log.info("[getBookById]: "+bookId+" 不存在");
            return Result.error(bookId+" 不存在");
        }
        // 这里无需去设置中文，前端是根据status来识别的
//        // 根据status来更新statusCN
//        bookInfo.setStatusCN(bookInfo.getStatus() == 0 ? "可借阅" : "不可借阅");
        return Result.success(bookInfo);
    }

    @Override
    public Result addBook(BookInfo bookInfo) {
        // 这里不考虑存在同名书籍其余信息不同的情况
        // 1、检验参数是否合法
        if (bookInfo == null) {
            log.info("[addBook]: 参数不合法");
            return Result.error("参数不合法");
        }
        if (!StringUtils.hasText(bookInfo.getBookName())
                || !StringUtils.hasText(bookInfo.getAuthor())
                || !StringUtils.hasText(bookInfo.getPublish())) {
            log.info("[addBook]: 参数不合法");
            return Result.error("参数不合法");
        }
        if (bookInfo.getCount() == null || bookInfo.getCount() < 0
                || bookInfo.getPrice() == null
                || bookInfo.getPrice().compareTo(new BigDecimal(0)) <= 0
                || bookInfo.getStatus() == null || bookInfo.getStatus() < 0
                || bookInfo.getStatus() == 1 || bookInfo.getStatus() > 2) {
            log.info("[addBook]: 参数不合法");
            return Result.error("参数不合法");
        }
        // 2、往数据库中插入新数据
        Integer result = bookMapper.addBook(bookInfo);
        if (result == 1) {
            log.info("[addBook]: id为"+bookInfo.getBookId()+"的图书新增成功");
            return Result.success(true);
        }
        log.info("[addBook]: 新增图书失败");
        return Result.error("新增图书失败");
    }

    // 添加事务，当抛出异常时，事务回滚
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result batchDeleteBook(List<Integer> bookIdList) {
        // 1、检验参数
        if (bookIdList == null || bookIdList.size() == 0) {
            log.info("[batchDeleteBook]: 参数不合法");
            return Result.error("参数不合法");
        }
        // 2、遍历列表删除
        for (Integer bookId : bookIdList) {
            bookMapper.deleteBook(bookId);
        }
        log.info("[batchDeleteBook]: 批量删除"+bookIdList.toString()+"成功");
        return Result.success(true);
    }

    // 根据 bookId 查询图书信息
    private BookInfo queryBookById(Integer bookId) {
        if (bookId == null || bookId <= 0) {
            return null;
        }
        return bookMapper.queryBookById(bookId);
    }
}
